package erp_services

import (
	"errors"
	"fmt"
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type stockService struct {
}

var StockService = new(stockService)

func (stockService *stockService) GetByUnioNo(unionNo string) (stock erp_models.Stock, err error) {
	err = global.App.DB.Where("union_no = ? ", unionNo).Find(&stock).Error

	if err != nil {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}

// 新增
func (stockService *stockService) Create(stock erp_models.Stock) (string, error) {
	result := global.App.DB.Create(&stock)
	if result.Error != nil {
		return "", result.Error
	}
	return stock.ID, nil
}

func (stockService *stockService) Update(stock erp_models.Stock) (err error) {
	if err := global.App.DB.Model(&erp_models.Stock{}).Where("ID = ?", stock.ID).Omit("ID", "UnionNo").Updates(&stock).Error; err != nil {
		return err
	}
	return nil
}

func (stockService *stockService) SetByMaterialName(unionNo string) (stock erp_models.Stock, err error) {
	err = global.App.DB.Where("union_no = ? ", unionNo).Find(&stock).Error

	if err != nil {
		err = errors.New("数据不存在")
	}
	return
}

// 库存消耗或增加
func (stockService *stockService) ConsumeStockByMaterialId(id uint, amount float64) (err error) {
	var stock erp_models.Stock
	if err = global.App.DB.Where("material_id = ?", id).First(&stock).Error; err != nil {
		err = errors.New("未查询到信息")
		return
	}
	val := fmt.Sprintf("%.2f", stock.CurrStock-amount)

	if err = global.App.DB.Model(&stock).Update("curr_stock", val).Error; err != nil {
		return
	}

	return nil
}

// 根据仓库ID更新库存信息
func (stockService *stockService) ConsumeStockById(id string, station_id int64, amount float64, countArgs map[string]float64) (err error) {
	var stock erp_models.Stock
	if err = global.App.DB.Where("mixing_station_id = ? AND id = ?", station_id, id).First(&stock).Error; err != nil {
		err = errors.New("未查询到信息")
		return
	}

	if countArgs["FactUse"] != 0 {
		factUseVal := fmt.Sprintf("%.5f", stock.TotalFactUse+countArgs["FactUse"])
		if err = global.App.DB.Model(&stock).Where("mixing_station_id = ?", station_id).Update("total_fact_use", factUseVal).Error; err != nil {
			return
		}
	}

	if countArgs["PbUse"] != 0 {
		pbUseVal := fmt.Sprintf("%.5f", stock.TotalPbUse+countArgs["PbUse"])
		if err = global.App.DB.Model(&stock).Where("mixing_station_id = ?", station_id).Update("total_pb_use", pbUseVal).Error; err != nil {
			return
		}
	}

	if countArgs["InStockNum"] != 0 {
		inStockNumVal := fmt.Sprintf("%.5f", stock.TotalIn+countArgs["InStockNum"])
		if err = global.App.DB.Model(&stock).Where("mixing_station_id = ?", station_id).Update("total_in", inStockNumVal).Error; err != nil {
			return
		}
	}

	val := fmt.Sprintf("%.5f", stock.CurrStock-amount)
	if err = global.App.DB.Model(&stock).Where("mixing_station_id = ?", station_id).Update("curr_stock", val).Error; err != nil {
		return
	}

	return nil
}

func (stockService *stockService) GetStockById(storage string, station_id int64) (stock *erp_models.Stock, err error) {
	err = global.App.DB.Where("mixing_station_id = ? AND id = ? ", station_id, storage).First(&stock).Error
	if err != nil || stock.ID == "" {
		err = errors.New("数据不存在")
	}
	return
}

func (stockService *stockService) GetStockByStockNo(stock_no string, station_id int64) (stock *erp_models.Stock, err error) {
	err = global.App.DB.Where("mixing_station_id = ? AND stock_no = ? ", station_id, stock_no).First(&stock).Error
	if err == nil && stock.ID == "" {
		err = errors.New("数据不存在")
	}
	return
}

// 根据原料id查询对应的仓库
func (stockService *stockService) GetStockByMaterialId(material_id int64) (stocks []erp_models.Stock, err error) {
	err = global.App.DB.Where("material_id = ? ", material_id).Find(&stocks).Error
	if err == nil && len(stocks) == 0 {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}

// 平台物资编号查询对应的仓库
func (stockService *stockService) GetStockByWZBH(material_no string, station_id int64) (stock *erp_models.Stock, err error) {
	err = global.App.DB.Where("FIND_IN_SET(?, platform_material_no) > 0 AND mixing_station_id = ?", material_no, station_id).First(&stock).Error
	if err != nil || stock.ID == "" {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}
